package com.dingding.study.service.防重复提交;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: spring-learning
 * @description:
 * @author: liugongding
 * @create: 2021-09-23 17:29
 **/

@EnableAspectJAutoProxy
@Component
@Aspect
@Slf4j
public class RepeatAccessDeniedAop {


    @Pointcut("@annotation(com.dingding.study.annotation.RepeatAccessDenied)")
    public void repeatAccessDeniedPointCut(){};

    @Around(value = "repeatAccessDeniedPointCut()")
    public Object repeatAccessDeniedAround(ProceedingJoinPoint joinPoint) throws Throwable {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        HttpServletRequest request = requestAttributes.getRequest();
        String url = request.getRequestURL().toString();
        String authType = request.getAuthType();
        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getTarget().getClass().getName();
        String params = JSON.toJSONString(joinPoint.getArgs());
        StringBuilder sb = new StringBuilder(url)
                .append(authType)
                .append(className)
                .append(methodName)
                .append(params);
        String cacheKey = DigestUtils.md5DigestAsHex(sb.toString().getBytes());
        Map<String, String> map = new HashMap<>();
        return null;
    }
}
